package cn.kent.service;

import cn.kent.entity.Product;
import cn.kent.mapper.ProductMapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.internal.runtime.PropertyMap;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> {

    @Qualifier("esRestClient")
    @Autowired
    private RestHighLevelClient client;

    // 添加索引
    public String insertIndex() throws IOException {
        final List<Product> list = this.list();
        final BulkRequest bulkRequest = new BulkRequest();
        for (Product product : list) {
            final IndexRequest indexRequest = new IndexRequest("baizhimall", "product", product.getId().toString());
            indexRequest.source(JSON.toJSONString(product), XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        final BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        for (BulkItemResponse item : bulk.getItems()) {
            System.out.println(item.getFailure());
        }
        return "success";
    }

    public List<Product> search(String p) throws IOException {
        List<Product> list = new ArrayList<>();
        final SearchRequest searchRequest = new SearchRequest("baizhimall");
        final SearchSourceBuilder searchRequestBuilder = new SearchSourceBuilder();
        searchRequestBuilder.query(QueryBuilders.matchQuery("name", p));

        searchRequest.source(searchRequestBuilder);

        final SearchResponse res = client.search(searchRequest, RequestOptions.DEFAULT);
        final SearchHits hits = res.getHits();
        for (SearchHit hit : hits) {
            final String sourceAsString = hit.getSourceAsString();
            System.out.println(sourceAsString);
            list.add(JSONObject.parseObject(sourceAsString, Product.class));
        }
        return list;
    }
}
